package com.item.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.Page;
import com.base.dialect.PaginationSupport;
import com.base.util.DateUtil;
import com.item.dao.model.RecommendMedia;
import com.item.dao.model.RecommendMediaExample;
import com.item.dao.model.RecommendMediaExample.Criteria;
import com.item.exception.JSONRepUtils;
import com.item.service.RecommendMediaService;
import com.item.util.SubjectUtils;

@Controller
public class MediaController {
	@Autowired
	private RecommendMediaService mediaService;
	
	@RequestMapping("/listMedia")
	@ResponseBody
	public String list(Page param,RecommendMedia media) throws Exception{
		RecommendMediaExample example = new RecommendMediaExample();
		PaginationSupport.setContext(param.getPageStart(), param.getPageSize(), null);
		Criteria criteria = example.createCriteria();
		
		//以下两个字段用来做筛选  分别是title和source的模糊匹配
		if(StringUtils.isNotBlank(media.getContent())){
			criteria.andContentLike("%"+media.getContent()+"%");
		}
		if(media.getDate() != null){
			criteria.andDateEqualTo(media.getDate());
		}
		example.setOrderByClause("date DESC");
		List<RecommendMedia> list = mediaService.selectByExample(example);
		return JSONRepUtils.serialize(new Page(PaginationSupport.getContext().getTotalCount(), list), 0);
	}
	
	@RequestMapping("/showlistMedia")
	public String list() {
		return "media/mediaList";
	}
	
	@RequestMapping("/toEditMedia")
	public String toEdit()throws Exception{
		return "/media/mediaEdit";
	}
	
	@RequestMapping(value="/saveMedia",method=RequestMethod.POST)
	@ResponseBody
	public String save(RecommendMedia media, HttpSession session)throws Exception{
		//WebPageFileController fileController = new WebPageFileController("bs", "news", "utf8");
//		System.out.println(date);
		
		try{
			if(StringUtils.isBlank(media.getId())){
				//首先检查这个日期的是否有了
				RecommendMediaExample rmex = new RecommendMediaExample();
				rmex.createCriteria().andDateEqualTo(media.getDate());
				List<RecommendMedia> lists = mediaService.selectByExample(rmex);
				if(lists != null && lists.size() > 0){
					return "{\"success\":date}";
				}
				media.setCreateTime(DateUtil.parseDate(media.getDate(), "yyyy-MM-dd"));
				media.setMusicFile("");
				media.setMusicSinger("");
				media.setMusicTitle("");
				media.setContent("成功路上并不拥挤 因为坚持的人不多");
				mediaService.insert(media);
			}else{
				media.setModifyTime(new Date());
				mediaService.updateByPrimaryKeySelective(media);
			}
		}catch(Exception e){
			e.printStackTrace();
			return "{\"success\":false}";
		}
		
		return "{\"success\":true}";
	}
	
	@RequestMapping(value="/findByIdMedia")
	@ResponseBody
	public String findById(String id,HttpSession session)throws Exception{		
		RecommendMedia n = mediaService.selectByPrimaryKey(id);
		return JSONRepUtils.serialize(n, 0);
	}
	
	@RequestMapping("/delMedia")
	@ResponseBody
	public String del(String id,String url,HttpSession session)throws Exception{
//		WebPageFileController fileController = new WebPageFileController();
		String[] ids = id.split(",");
		int len = ids.length;
		for(int i = 0; i < len; i++) {
			mediaService.deleteByPrimaryKey(ids[i]);
//			fileController.deleteFile(urls[i]);
		}
		return "{\"success\":true}";
	}
	
	@RequestMapping("/topSetMedia")
	@ResponseBody
	public String topSet(String id,HttpSession session) throws Exception {
		return "{\"success\":true}";
	}
	
	@RequestMapping("/topDelMedia")
	@ResponseBody
	public String topDel(String id,HttpSession session) throws Exception {
		return "{\"success\":true}";
	}
}